﻿using D20.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using D20.Entities.Characters;
using System;
namespace D20.UnitTests.Characters
{
    
    
    /// <summary>
    ///This is a test class for EntityTest and is intended
    ///to contain all EntityTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EntityTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        internal virtual Entity CreateEntity()
        {
            Entity target = new PlayerCharacter();
            return target;
        }

        /// <summary>
        ///A test for Revive
        ///</summary>
        [TestMethod()]
        public void ReviveTest()
        {
            Entity target = CreateEntity();
            Entity cause = CreateEntity();
            target.ApplyHealthModification(cause, -target.Health);
            Assert.IsTrue(target.Health <= 0, "The target is not dead yet.");
            target.Revive(cause);
            Assert.IsTrue(target.Health > 0, "The target is still dead.");
        }

        /// <summary>
        ///A test for ApplyHealthModification
        ///</summary>
        [TestMethod()]
        public void ApplyHealthModificationTest()
        {
            Entity target = CreateEntity();
            Entity cause = null;
            target.MaxHealth = 9999999;

            float originalHealth = target.Health;            
            float additiveModification = 12345;

            target.ApplyHealthModification(cause, additiveModification);
            Assert.AreEqual(originalHealth + additiveModification, target.Health);

            additiveModification = -12345;

            target.ApplyHealthModification(cause, additiveModification);
            Assert.AreEqual(originalHealth, target.Health);

            target.Health = 1;
            Assert.IsTrue(target.Health == 1);

            additiveModification = -1;

            target.OnDeath += new OnDeathEventHandler(target_OnDeath);
            target.ApplyHealthModification(cause, additiveModification);

            Assert.IsTrue(targetDied);

            bool alreadyDeadThrown = false;
            try
            {
                target.ApplyHealthModification(cause, +1);
            }
            catch (AlreadyDeadException)
            {
                alreadyDeadThrown = true;
            }
            Assert.IsTrue(alreadyDeadThrown);

            target.OnRevive += new OnReviveEventHandler(target_OnRevive);
            target.Revive(cause);
            Assert.IsTrue(targetRevived);

            additiveModification = 0;
            bool zeroModificationThrown = false;
            try
            {
                target.ApplyHealthModification(cause, additiveModification);
            }
            catch (ArgumentOutOfRangeException)
            {
                zeroModificationThrown = true;
            }
            Assert.IsTrue(zeroModificationThrown);
        }

        void target_OnRevive(ReviveEventArgs e)
        {
            targetRevived = true;   
        }
        private bool targetDied, targetRevived;
        void target_OnDeath(DeathEventArgs e)
        {
            targetDied = true;
        }
    }
}
